package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constants.user.UserConstants;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.fegin.AuthorFegin;
import com.usian.user.fegin.WmUserFegin;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/24 14:03
 */
@Service
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper,ApUserRealname>
implements ApUserRealnameService {


    @Autowired(required = false)
    AuthorFegin authorFegin;

    @Autowired(required = false)
    WmUserFegin wmUserFegin;

    @Autowired(required = false)
    ApUserMapper apUserMapper;





    @Override
    public ResponseResult findUserRealNameList(AuthDto authDto) {
        if(null==authDto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        authDto.checkParam();

        QueryWrapper<ApUserRealname> objectQueryWrapper = new QueryWrapper<ApUserRealname>();

        if(authDto.getStatus()!=null){
            objectQueryWrapper.lambda().eq(ApUserRealname::getStatus,authDto.getStatus());
        }

        Page<ApUserRealname> page = new Page<>(authDto.getPage(),authDto.getSize());


        IPage<ApUserRealname> page1 = this.page(page, objectQueryWrapper);
       ResponseResult responseResult =
                new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) page1.getTotal());

        responseResult.setData(page1.getRecords());

        return responseResult;
    }

    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto authDto) {

        //检查参数
        if(null==authDto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //审核：修改，实名认证表
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(authDto.getStatus());
        if(null!=authDto.getMsg()){
            apUserRealname.setReason(authDto.getMsg());
        }
        this.baseMapper.updateById(apUserRealname);

        //审核通过需要远程调用
        if(authDto.getStatus().equals(UserConstants.PASS_AUTH)){
            saveWmUserAndAuthor(authDto);
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 调用自媒体的微服务
     * @param authDto
     */
    public void saveWmUserAndAuthor( AuthDto authDto){
        Integer id = authDto.getId();
        ApUserRealname apUserRealname = this.baseMapper.selectById(id);
        Integer userId = apUserRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        String name = apUser.getName();
        WmUser wmUserByName = wmUserFegin.findWmUserByName(name);
        if(null==wmUserByName){
            WmUser wmUser = new WmUser();
            wmUser.setPassword(apUser.getPassword());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(name);
            wmUser.setCreatedTime(new Date());
            wmUser.setStatus(UserConstants.PASS_AUTH.intValue());
            wmUser.setApUserId(apUser.getId());
            wmUser.setType(UserConstants.WM_USER_TYPE);
            wmUserFegin.saveWmUser(wmUser);

            log.info("000000000000");

            //创建作者信息
            createAuthor(apUser,wmUser);
            log.info("11111111111111111");
            apUser.setFlag(UserConstants.AP_USER_FLAG);
            apUserMapper.updateById(apUser);

        }

    }


    /**
     * 创建个作者的账户，
     * @param apUser
     * @param wmUser
     */
    public void createAuthor(ApUser apUser,WmUser wmUser){
        ApAuthor author = authorFegin.findByUserid(apUser.getId());
        log.info("author:{}",author);
        if(null==author){
            log.info("=========================");
            ApAuthor apAuthor = new ApAuthor();
            apAuthor.setName(apUser.getName());
            apAuthor.setCreatedTime(new Date());
            apAuthor.setType(UserConstants.AP_AUTHOR_TYPE);
            apAuthor.setUserId(apUser.getId());
            apAuthor.setWmUserId(wmUser.getId());
            authorFegin.save(apAuthor);
        }

    }


}
